package com.chanjiedata.changbs.module.main;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.adapter.MyMachineAdapter;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Mystock;
import com.chanjiedata.changbs.utlis.GsonUtils;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.chanjiedata.changbs.utlis.ValidateUtils;
import com.google.protobuf.InvalidProtocolBufferException;
import com.ruffian.library.widget.RTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by AndroidIntexh1 on 2018/12/27.
 */

public class MyMachineActivity extends BaseActivity {
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.aad_tv)
    TextView aadTv;
    @BindView(R.id.tv_allot)
    RTextView tvAllot;
    private MyMachineAdapter mAdapter;
    private List<Mystock.myMachine.List> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_machin);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        initRecyclerView();
        initListener();
        tvTitle.setText("库存管理");

    }

    private void initListener() {
        backIv.setOnClickListener(view -> finish());
        aadTv.setOnClickListener(view -> startActivity(AllowLogNewActivity.class));
        tvAllot.setOnClickListener(view -> {
            String intentData = GsonUtils.serializedToJson(datas);
            Intent intent = new Intent(mContext, AllocaActivity.class);
            intent.putExtra("data", intentData);
            startActivity(intent);
        });
    }

    private void initData() {
        NetworkManager.INSTANCE.post(Apis.myMachine, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                datas = Mystock.myMachine.parseFrom(bytes).getListList();
                if (ValidateUtils.isValidate(datas)) {
                    mAdapter.replaceData(datas);
                    tvAllot.setEnabled(true);
                } else {
                    mAdapter.replaceData(datas);
                    mAdapter.notifyDataSetChanged();
                    tvAllot.setEnabled(false);
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
                tvAllot.setEnabled(false);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();

    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new MyMachineAdapter(mContext, datas));
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.setEmptyView(R.layout.recycle_empty);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mContext, PartnerMachineDetailActivity.class);
            intent.putExtra("goods_id", datas.get(position).getGoodsId());
            startActivity(intent);
        });
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tv_fen_run) {
                Intent intent = new Intent(mContext, ShareProfitActivity.class);
                intent.putExtra("goods_id", datas.get(position).getGoodsId());
                intent.putExtra("title", "分润细则");
                startActivity(intent);
            }
        });
    }
}
